//
//  DestTraveNotesPhotoesViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15/10/29.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "DestTraveNotesPhotoesViewController.h"
#import "BaseHeader.h"
#import "DestTravePhotoesCollectionViewCell.h"
#import "DestTravePhotoesModel.h"

@interface DestTraveNotesPhotoesViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *destCollectionView;
@property(nonatomic,strong)NSMutableArray *DataArray;
@property(nonatomic,strong)UILabel *dayLabel;
@property(nonatomic,strong)UIView *headerView;
@property(nonatomic,strong)UIView *baseView;
@property(nonatomic,strong)UIView *footerView;
@property(nonatomic,strong)UIView *footBaseView;
@property(nonatomic,strong)UITextView *moreTextView;
@property(nonatomic,strong)UILabel *textLabel;
@property(nonatomic,strong)UILabel *cityLabel;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)UIImageView *mapImageView;
@property(nonatomic,strong)UIImageView *timeImageView;
@property(nonatomic,assign)BOOL YesOrNo;
@property(nonatomic,strong)UIButton *button;
@property(nonatomic,assign)NSInteger Row;
@property(nonatomic,assign)NSInteger whatRow;
@property(nonatomic,assign)NSInteger CollectionIndex;
@property(nonatomic,strong)UIButton *GetDataButton;
@end

@implementation DestTraveNotesPhotoesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _YesOrNo = YES;
    _Row = 0;
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.navigationController.navigationBarHidden =  YES;
    self.tabBarController.tabBar.hidden = YES;
    _DataArray = [[NSMutableArray alloc]init];
    _GetDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _GetDataButton.frame = CGRectMake(10*WIDTH, 35*HEIGHT, 35 *WIDTH, 35*HEIGHT);
    [_GetDataButton setImage:[UIImage imageNamed:@"play-jiantouzuo.png"] forState:UIControlStateNormal];
    [_GetDataButton addTarget:self action:@selector(DestGoBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_GetDataButton];
    [self getDATA];
    
    
    
}
- (void)createOtherControll {
    _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100 * HEIGHT)];
    _baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100 * HEIGHT)];
    _baseView.backgroundColor = [UIColor clearColor];
    [_headerView addSubview:_baseView];
    _headerView.backgroundColor = [UIColor blackColor];
    _headerView.alpha  = 0.6f;
    [self.view addSubview:_headerView];
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(10 * WIDTH,35 * HEIGHT, 35 *HEIGHT,35 * HEIGHT);
    [_button setImage:[UIImage imageNamed:@"play-jiantouzuo.png"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(DestGoBack) forControlEvents:UIControlEventTouchUpInside];
    
    self.dayLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,49 * HEIGHT)];
    self.dayLabel.font = [UIFont systemFontOfSize:19.];
    self.dayLabel.textColor = [UIColor whiteColor];
    self.dayLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.dayLabel];
    [self.view addSubview:_button];
    
    _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-150 * HEIGHT, SCREEN_WIDTH, 150 * HEIGHT)];
    _footerView.backgroundColor = [UIColor blackColor];
    _footerView.alpha = 0.6;
    _footBaseView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH-150 * HEIGHT,150 * HEIGHT)];
    _footBaseView.backgroundColor = [UIColor clearColor];
    [_footerView addSubview:_footBaseView];
    [self.view addSubview:_footerView];
    _moreTextView = [[UITextView alloc]initWithFrame:CGRectMake(10 *WIDTH, SCREEN_HEIGHT - 140 *HEIGHT, SCREEN_WIDTH - 20 *WIDTH, 80* HEIGHT)];
    _moreTextView.scrollEnabled = YES;
    _moreTextView.editable = NO;
    _moreTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _moreTextView.backgroundColor = [UIColor clearColor];
    _moreTextView.textColor = [UIColor whiteColor];
    _moreTextView.font = [UIFont systemFontOfSize:17.];
 
    [self.view addSubview:_moreTextView];
    _mapImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10 *WIDTH, SCREEN_HEIGHT - 30 * HEIGHT, 20 *WIDTH, 20 * HEIGHT)];
    [self.view addSubview:_mapImageView];
    _cityLabel = [[UILabel alloc]initWithFrame:CGRectMake(30 * WIDTH, SCREEN_HEIGHT- 40 * HEIGHT, 150 * WIDTH , 40 * HEIGHT)];
    _cityLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_cityLabel];
    _timeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(160 * WIDTH, SCREEN_HEIGHT - 50 * HEIGHT, 20 *WIDTH, 20 * HEIGHT)];
    [self.view addSubview:_timeImageView];
    _timeLabel  = [[UILabel alloc]initWithFrame:CGRectMake(180 * WIDTH, SCREEN_HEIGHT - 60 * HEIGHT, 180 * WIDTH, 40 * HEIGHT)];
    _timeLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_timeLabel];
}
#pragma mark - 返回方法
- (void)DestGoBack {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH , SCREEN_HEIGHT);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing =0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _destCollectionView  = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    _destCollectionView.backgroundColor = [UIColor blackColor];
    _destCollectionView.bounces = NO;
    _destCollectionView.delegate = self;
    _destCollectionView.dataSource =self;
    _destCollectionView.pagingEnabled = YES;
    _destCollectionView.showsHorizontalScrollIndicator = NO;
    [_destCollectionView registerClass:[DestTravePhotoesCollectionViewCell class] forCellWithReuseIdentifier:@"destTravePhotoesCollectionViewCell"];
    [self.view addSubview:_destCollectionView];
}
- (void)getDATA{
    [DestAFNDataBlock string:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/",self.trip_id] AFNData:^(id block) {
        NSMutableArray *DaysArr = [block objectForKey:@"days"];
        [self createCollectionView];
        [self createOtherControll];
       
        
        
        for (NSMutableDictionary *DaysDic in DaysArr) {
            
            NSMutableArray *wayPointsArr = [DaysDic objectForKey:@"waypoints"];
            for (NSMutableDictionary *wayPointSDic in wayPointsArr) {
                DestTravePhotoesModel *traveModel = [DestTravePhotoesModel modelWithDictionary:wayPointSDic];
                [_DataArray addObject:traveModel];
            }
        }
        ///判断下点击进来图片所在位置
        for (NSInteger a =0; a<_DataArray.count; a++) {
            DestTravePhotoesModel *model = [_DataArray objectAtIndex:a];
           
            if ([_pushPhoto isEqualToString:model.photo]) {
                _whatRow = a;
            }
        }

        
     DestTravePhotoesModel *model = [_DataArray objectAtIndex:_whatRow];
        _cityLabel.text = [NSString stringWithFormat:@"%@,%@",model.province,model.city];
        _timeLabel.text = model.local_time;
       _moreTextView.text = model.text;
       self.dayLabel.text =[NSString stringWithFormat:@"第%@天 %ld/%ld",model.day,_Row +1,_DataArray.count];
        _timeImageView.image = [UIImage imageNamed:@"DestShiZhong.png"];
        _mapImageView.image = [UIImage imageNamed:@"DestMyMap.png"];
        [_destCollectionView reloadData];
      
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_whatRow inSection:0];
        [_destCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    } AFNData1:^(id block) {
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _DataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DestTravePhotoesCollectionViewCell *travePhotoesCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"destTravePhotoesCollectionViewCell" forIndexPath:indexPath];
    DestTravePhotoesModel *model = [_DataArray objectAtIndex:indexPath.row];
    
    travePhotoesCell.dataModel = model;
    _Row = indexPath.row;

    travePhotoesCell.backgroundColor = [UIColor blackColor];
    travePhotoesCell.imageDelegate = self;
    return travePhotoesCell;
}



- (void)imageTap {
    if (_YesOrNo == NO) {
        [UIView animateWithDuration:0.5 animations:^{
            
            _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100 * HEIGHT);
            _baseView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100 * HEIGHT);
            _footerView.frame =CGRectMake(0, SCREEN_HEIGHT - 150 * HEIGHT, SCREEN_WIDTH, 150 * HEIGHT);
            _footBaseView.frame = CGRectMake(0, SCREEN_HEIGHT- 150 * HEIGHT, SCREEN_WIDTH, 150 * HEIGHT);
            _button.frame =CGRectMake(10 * WIDTH, 35 * HEIGHT, 35 * WIDTH, 35 * HEIGHT);
            self.dayLabel.frame =CGRectMake(0, 25* HEIGHT, SCREEN_WIDTH, 49 * HEIGHT);
            _moreTextView.frame = CGRectMake(0, SCREEN_HEIGHT - 140* HEIGHT, SCREEN_WIDTH - 20 * WIDTH, 80* HEIGHT);
            _cityLabel.frame = CGRectMake(30 * WIDTH, SCREEN_HEIGHT - 60 * HEIGHT, 150 * WIDTH, 40 * HEIGHT);
            _timeLabel.frame = CGRectMake(180 * WIDTH, SCREEN_HEIGHT  - 60 * HEIGHT, 180 * WIDTH, 40 * HEIGHT);
            _mapImageView.frame = CGRectMake(10 * WIDTH, SCREEN_HEIGHT - 50 * HEIGHT, 20 *WIDTH, 20 * HEIGHT);
            _timeImageView.frame = CGRectMake(160 * WIDTH, SCREEN_HEIGHT - 50 * HEIGHT, 20 * WIDTH, 20 * HEIGHT);
        }];
        _YesOrNo = YES;
    }else{
        
        [UIView animateWithDuration:0.5 animations:^{
            _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40 * HEIGHT);
            _baseView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40 * HEIGHT);
            _button.frame = CGRectMake(10 * HEIGHT, 0, 35 * WIDTH, 0);
            _dayLabel.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0);
            _footerView.frame = CGRectMake(0, SCREEN_HEIGHT -40 * HEIGHT, SCREEN_WIDTH, 40 * HEIGHT);
            
            _footBaseView.frame = CGRectMake(0, SCREEN_HEIGHT - 40 * HEIGHT, SCREEN_WIDTH, 40 * HEIGHT);
            _moreTextView.frame = CGRectMake(10, SCREEN_HEIGHT - 30 * HEIGHT, SCREEN_WIDTH - 20 * WIDTH, 0);
            _timeLabel.frame  = CGRectMake(180 * WIDTH, SCREEN_HEIGHT - 40 * HEIGHT, 180 * WIDTH, 40 * HEIGHT);
            _cityLabel.frame = CGRectMake(30 * WIDTH, SCREEN_HEIGHT- 40 * HEIGHT, 150 * WIDTH , 40 * HEIGHT);
            _mapImageView.frame = CGRectMake(10 * WIDTH, SCREEN_HEIGHT - 30 * HEIGHT, 20 * WIDTH, 20 * HEIGHT);
            _timeImageView.frame = CGRectMake(160 * WIDTH, SCREEN_HEIGHT - 30 * HEIGHT, 20 * WIDTH, 20 * HEIGHT);
        }];
        
        
        _YesOrNo = NO;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (_DataArray.count != 0) {
        
        
        //这句话 很重要 说三遍 很重要 很重要 很重要
        int halfX = scrollView.frame.size.width / 2;
        _CollectionIndex = ((_destCollectionView.contentOffset.x - self.destCollectionView.frame.size.width - halfX) / _destCollectionView.frame.size.width) +  1;
        if (!_CollectionIndex) {
            _CollectionIndex = -1;
        }
        DestTravePhotoesModel *model1 = [_DataArray objectAtIndex:_CollectionIndex  + 1];
        
        self.dayLabel.text =[NSString stringWithFormat:@"第%@天 %ld/%ld",model1.day,_CollectionIndex +2 ,_DataArray.count];
        _moreTextView.text = model1.text;
        if ([model1.province isEqualToString:@""]&& [model1.city isEqualToString:@""]) {
            _cityLabel.text = @"";
            _mapImageView.image = [UIImage imageNamed:@""];
        } else {
             _cityLabel.text = [NSString stringWithFormat:@"%@,%@",model1.province,model1.city];
            _mapImageView.image = [UIImage imageNamed:@"DestMyMap.png"];
        }
            _timeImageView.image = [UIImage imageNamed:@"DestShiZhong.png"];
       
        
        _timeLabel.text =[model1.local_time substringToIndex:16];
      
        
    }
    

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
